package com.xingyun.servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xingyun.pojo.Grade;
import com.xingyun.pojo.Student;
import com.xingyun.service.grade.GradeService;
import com.xingyun.service.grade.impl.GradeServiceImpl;
import com.xingyun.service.student.StudentService;
import com.xingyun.service.student.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.ref.ReferenceQueue;
import java.util.List;

public class StudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf8");

        PrintWriter out = response.getWriter();
        GradeService gradeService = new GradeServiceImpl();
        StudentService studentService = new StudentServiceImpl();

        //每页显示的数据数量
        Integer pageSize = 3;

        String flag = request.getParameter("flag");

        if (flag != null) {

            //分页
            if (flag.equals("paging")) {
                String page = request.getParameter("pageNum");
                PageHelper.startPage(Integer.parseInt(page), pageSize);
                List<Student> list = studentService.findAll();
                PageInfo<Student> pageInfo = new PageInfo<>(list);

                //当前页码
                Integer pageNum = pageInfo.getPageNum();
                //总页码
                Integer pages = pageInfo.getPages();

                //年级
                List<Grade> grades = gradeService.findAll();
                request.setAttribute("grades", grades);

                request.setAttribute("pageInfo", pageInfo);
                request.setAttribute("pageNum", pageNum);
                request.setAttribute("pages", pages);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }

            //模糊查询
            if (flag.equals("find")) {
                String name = request.getParameter("name");
                String grade = request.getParameter("grade");

                PageHelper.startPage(1, pageSize);
                List<Student> list = studentService.findByNameAndGradeId(name, grade);
                PageInfo<Student> pageInfo = new PageInfo<>(list);
                //当前页码
                Integer pageNum = pageInfo.getPageNum();
                //总页码
                Integer pages = pageInfo.getPages();

                //年级
                List<Grade> grades = gradeService.findAll();
                request.setAttribute("grades", grades);

                request.setAttribute("pageInfo", pageInfo);
                request.setAttribute("pageNum", pageNum);
                request.setAttribute("pages", pages);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }

            //删除
            if (flag.equals("del")) {
                String id = request.getParameter("id");
                int count = studentService.delById(Integer.parseInt(id));
                out.println(count);
                out.flush();
                out.close();
            }

            //跳转到添加页面
            if (flag.equals("addPage")) {
                //年级
                List<Grade> grades = gradeService.findAll();
                request.setAttribute("grades", grades);
                request.setAttribute("choice", "添加");
                request.getRequestDispatcher("add.jsp").forward(request, response);
            }

            //添加
            if (flag.equals("添加")) {
                Student student = new Student();
                student.setName(request.getParameter("name"));
                student.setSex(request.getParameter("sex"));
                student.setGradeId(Integer.parseInt(request.getParameter("grade")));
                int count = studentService.save(student);

                if (count > 0) {
                    PageHelper.startPage(1, pageSize);
                    List<Student> list = studentService.findAll();
                    PageInfo<Student> pageInfo = new PageInfo<>(list);

                    //当前页码
                    Integer pageNum = pageInfo.getPageNum();
                    //总页码
                    Integer pages = pageInfo.getPages();

                    //年级
                    List<Grade> grades = gradeService.findAll();
                    request.setAttribute("grades", grades);

                    request.setAttribute("pageInfo", pageInfo);
                    request.setAttribute("pageNum", pageNum);
                    request.setAttribute("pages", pages);
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                }
            }

            //跳转到修改页面
            if (flag.equals("updPage")) {
                //年级
                List<Grade> grades = gradeService.findAll();
                //学生信息
                Student student = studentService.findOne(Integer.parseInt(request.getParameter("id")));
                request.setAttribute("student", student);
                request.setAttribute("grades", grades);
                request.setAttribute("choice", "修改");
                request.getRequestDispatcher("add.jsp").forward(request, response);
            }

            //修改
            if (flag.equals("修改")) {
                Student student = new Student();
                student.setId(Integer.parseInt(request.getParameter("id")));
                student.setName(request.getParameter("name"));
                student.setSex(request.getParameter("sex"));
                student.setGradeId(Integer.parseInt(request.getParameter("grade")));
                int count = studentService.upd(student);

                if (count > 0) {
                    PageHelper.startPage(1, pageSize);
                    List<Student> list = studentService.findAll();
                    PageInfo<Student> pageInfo = new PageInfo<>(list);

                    //当前页码
                    Integer pageNum = pageInfo.getPageNum();
                    //总页码
                    Integer pages = pageInfo.getPages();

                    //年级
                    List<Grade> grades = gradeService.findAll();
                    request.setAttribute("grades", grades);

                    request.setAttribute("pageInfo", pageInfo);
                    request.setAttribute("pageNum", pageNum);
                    request.setAttribute("pages", pages);
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                }
            }

        } else {
            //如果flag为空
            PageHelper.startPage(1, pageSize);
            List<Student> list = studentService.findAll();
            PageInfo<Student> pageInfo = new PageInfo<>(list);

            //当前页码
            Integer pageNum = pageInfo.getPageNum();
            //总页码
            Integer pages = pageInfo.getPages();

            //年级
            List<Grade> grades = gradeService.findAll();
            request.setAttribute("grades", grades);

            request.setAttribute("pageInfo", pageInfo);
            request.setAttribute("pageNum", pageNum);
            request.setAttribute("pages", pages);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
}
